package com.duobaoyu.dby.supply.admin.controller.config;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.duobaoyu.dby.channel.adapter.enums.OperationTypeEnum;
import com.duobaoyu.dby.common.core.util.R;
import com.duobaoyu.dby.common.core.validator.ValidationList;
import com.duobaoyu.dby.common.core.validator.groups.Add;
import com.duobaoyu.dby.common.core.validator.groups.Edit;
import com.duobaoyu.dby.supply.admin.annotation.SupplySysLog;
import com.duobaoyu.dby.supply.biz.client.resource.ResourceStateInfoAdminClient;
import com.duobaoyu.dby.supply.biz.client.resource.bean.dto.StateBatchDTO;
import com.duobaoyu.dby.supply.biz.client.resource.bean.dto.StateDTO;
import com.duobaoyu.dby.supply.biz.client.resource.bean.vo.MappingBatchVO;
import com.duobaoyu.dby.supply.biz.client.resource.bean.vo.MappingVO;
import com.duobaoyu.dby.supply.biz.client.resource.bean.vo.StateInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 状态定义
 *
 * @author jz
 * @version 1.0
 * @date 2022/6/9 10:49
 */
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/state-info")
@Api(value = "state-info", tags = "状态定义")
public class StateInfoController {

	private final ResourceStateInfoAdminClient resourceStateInfoAdminClient;

	/**
	 * 批量查询状态
	 * @param stateBatchs
	 * @return
	 */
	@PostMapping("/state/list")
	public R<List<MappingBatchVO>> stateCodeList(@Validated @RequestBody ValidationList<StateBatchDTO> stateBatchs) {

		return R.ok(resourceStateInfoAdminClient.stateCodeList(stateBatchs));
	}


	/**
	 * 添加状态定义信息
	 *
	 * @param state
	 * @return boolean
	 */
	@PostMapping("/create")
	@ApiOperation(value = "添加状态定义", notes = "添加状态定义信息")
	@SupplySysLog(value = "添加状态定义信息",operationType = OperationTypeEnum.INTERT)
	public R<Boolean> add(@Validated(Add.class) @RequestBody StateDTO state) {

		return R.ok(resourceStateInfoAdminClient.addStateInfo(state));
	}

	/**
	 * 修改状态定义
	 *
	 * @param state
	 * @return boolean
	 */
	@PostMapping("/update")
	@ApiOperation(value = "修改状态定义", notes = "修改状态定义信息")
	@SupplySysLog(value = "修改状态定义信息",operationType = OperationTypeEnum.UPDATE)
	public R<Boolean> update(@Validated(Edit.class) @RequestBody StateDTO state) {

		return R.ok(resourceStateInfoAdminClient.updateStateInfo(state));
	}

	/**
	 * 删除状态定义
	 *
	 * @param id
	 * @return boolean
	 */
	@DeleteMapping("/delete/{id}")
	@ApiOperation(value = "删除状态定义", notes = "删除状态定义信息")
	@SupplySysLog(value = "删除状态定义信息",operationType = OperationTypeEnum.DELETE)
	public R<Boolean> delete(@NotNull(message = "状态信息ID不能为空") @PathVariable("id") Long id) {

		return R.ok(resourceStateInfoAdminClient.deleteStateInfo(id));
	}

	/**
	 * 查询状态定义信息详情
	 *
	 * @param id
	 * @return
	 */
	@GetMapping("/{id}")
	@ApiOperation(value = "查询状态定义信息详情", notes = "根据id查询状态定义信息详情")
	@SupplySysLog(value = "根据id查询状态定义信息详情",operationType = OperationTypeEnum.SELECT)
	public R<StateInfoVO> queryOne(@NotNull(message = "状态信息ID不能为空") @PathVariable("id") Long id) {

		return R.ok(resourceStateInfoAdminClient.queryStateInfoDetail(id));
	}

	/**
	 * 分页查询状态定义信息
	 *
	 * @param state
	 * @return
	 */
	@PostMapping("/page")
	@ApiOperation(value = "分页查询状态定义信息", notes = "分页查询状态定义信息")
	@SupplySysLog(value = "分页查询状态定义信息",operationType = OperationTypeEnum.SELECT)
	public R<Page<StateInfoVO>> page(@RequestBody StateDTO state) {

		return R.ok(resourceStateInfoAdminClient.queryStateInfoPage(state));
	}


	/**
	 * 获取字典映射
	 */
	@Deprecated
	@GetMapping("/getState/code")
	@ApiOperation(value = "获取字典映射", notes = "获取字典映射")
	@SupplySysLog(value = "获取字典映射",operationType = OperationTypeEnum.SELECT)
	public R<List<MappingVO>> getStateCode(@RequestParam("stateCode") String stateCode, String label, String filterName) {
		List<MappingVO> mappings = resourceStateInfoAdminClient.getStateInfoToLocalMappingCode(stateCode, label);
		if(StringUtils.isNotBlank(filterName)) {
			mappings = mappings.stream().filter(mapping -> mapping.getName().toString().contains(filterName)).collect(Collectors.toList());
		}
		return R.ok(mappings);
	}

}
